import CodeMirrorWrapper from '@/components/CodeMirrorWrapper';
import ToolPageTemplate from '@/components/ToolPageTemplate';
import { useAddress } from '@/hooks/tools/useAddress';
import BusinessIcon from '@mui/icons-material/Business';
import LocationOnIcon from '@mui/icons-material/LocationOn';
import SearchIcon from '@mui/icons-material/Search';
import {
    Alert,
    Box,
    Button,
    Chip,
    CircularProgress,
    Paper,
    Snackbar,
    Stack,
    Typography
} from '@mui/material';

/**
 * 企业地址查询工具页面
 */
const AddressPage = () => {
    const {
        address,
        result,
        error,
        isProcessing,
        setAddress,
        handleSubmit,
        clearError,
        getProgressText
    } = useAddress();

    return (
        <ToolPageTemplate
            title="企业地址查询工具"
            description="快速查找企业的详细地址信息"
            category="查询工具"
            categoryColor="#FF85C0"
        >
            {/* 快速操作区域 */}
            <Box sx={{ mb: 4 }}>
                <Stack direction="row" spacing={2} justifyContent="center" flexWrap="wrap">
                    <Chip
                        icon={<BusinessIcon />}
                        label="企业查询"
                        variant="outlined"
                        color="primary"
                    />
                    <Chip
                        icon={<LocationOnIcon />}
                        label="地址定位"
                        variant="outlined"
                        color="secondary"
                    />
                </Stack>
            </Box>

            <Stack spacing={4}>
                {/* 查询输入区域 */}
                <Paper
                    elevation={0}
                    sx={{
                        p: 3,
                        borderRadius: 3,
                        border: '1px solid',
                        borderColor: 'divider',
                        width: '100%'
                    }}
                >
                    <Typography
                        variant="h6"
                        sx={{
                            mb: 3,
                            fontWeight: 700,
                            display: 'flex',
                            alignItems: 'center',
                            gap: 1
                        }}
                    >
                        <BusinessIcon color="primary" />
                        企业信息查询
                    </Typography>

                    <Box sx={{ borderRadius: 2, overflow: 'hidden', border: '1px solid', borderColor: 'divider', mb: 3 }}>
                        <CodeMirrorWrapper
                            label=""
                            value={address}
                            onChange={setAddress}
                            height="200px"
                            language="text"
                            placeholder="请输入企业名称，支持模糊搜索..."
                        />
                    </Box>

                    <Box sx={{ textAlign: 'center' }}>
                        <Button
                            variant="contained"
                            size="large"
                            onClick={handleSubmit}
                            disabled={isProcessing || !address.trim()}
                            startIcon={isProcessing ? <CircularProgress size={20} /> : <SearchIcon />}
                            sx={{
                                borderRadius: '25px',
                                px: 4,
                                py: 1.5,
                                textTransform: 'none',
                                fontWeight: 600,
                                fontSize: '1.1rem',
                                minWidth: 200
                            }}
                        >
                            {getProgressText()}
                        </Button>
                    </Box>
                </Paper>

                {/* 查询结果区域 */}
                {result && (
                    <Paper
                        elevation={0}
                        sx={{
                            p: 3,
                            borderRadius: 3,
                            border: '1px solid',
                            borderColor: 'divider',
                            width: '100%'
                        }}
                    >
                        <Typography
                            variant="h6"
                            sx={{
                                mb: 3,
                                fontWeight: 700,
                                display: 'flex',
                                alignItems: 'center',
                                gap: 1
                            }}
                        >
                            <LocationOnIcon color="primary" />
                            地址信息
                        </Typography>

                        <Box sx={{ borderRadius: 2, overflow: 'hidden', border: '1px solid', borderColor: 'divider' }}>
                            <CodeMirrorWrapper
                                label=""
                                value={result}
                                onChange={() => { }}
                                readOnly={true}
                                height="200px"
                                language="json"
                            />
                        </Box>
                    </Paper>
                )}
            </Stack>

            {/* 错误提示 */}
            <Snackbar
                open={!!error}
                autoHideDuration={6000}
                onClose={clearError}
                anchorOrigin={{ vertical: 'top', horizontal: 'center' }}
            >
                <Alert severity="error" onClose={clearError} sx={{ width: '100%' }}>
                    {error}
                </Alert>
            </Snackbar>

            {/* 成功提示 */}
            <Snackbar
                open={!!(result && !error)}
                autoHideDuration={3000}
                anchorOrigin={{ vertical: 'bottom', horizontal: 'center' }}
            >
                <Alert severity="success" sx={{ width: '100%' }}>
                    查询成功！已获取企业地址信息
                </Alert>
            </Snackbar>
        </ToolPageTemplate>
    );
};

export default AddressPage;
